/* 
 *  @library GML(Global Market Library)
 *  Author: Alex Hu
 *
 */

'use strict';

/* Initiation GML */
var GML = {};

/* Treasures */
GML.Treasures = [];

/* Locale */
GML.L = [];

/* Room */
GML.Room = [];
GML.Room.roomNO = 1;
GML.Room.checkin = function(d){
    var roomNO = GML.Room.roomNO;
    GML.Room[roomNO] = d;
    GML.Room.roomNO++;
    return roomNO;
}

GML.RT = $('#royalTheater');

/* Global variable declare */
var doc = document;

/* Checkbox GUI */
$(doc).on('click', '[role~=checkbox]', function(e){
    var $this = $(this)
        , $input = $this.data('input')
        ;

    if(!$input){
        $input = $('<input />', {
            'type': 'checkbox',
            'name': $this.data('name'),
            'value': true,
            'class': 'hide'
        });
        $this.data('input', $input);
        $input.insertAfter($this);
    }
    if($this.hasClass('ico-chked')){
        $this.removeClass('ico-chked');
        $input[0].checked = false;
    }else{
        $this.addClass('ico-chked');
        $input[0].checked = true;
    }
    e.preventDefault();
});

/* GML GUI */
GML.GUI = {};

/* Select GUI */
GML.GUI['select'] = function(b){
    var $boss = b ? b : GML.RT
        , sheep = $boss.find('[role~=select]')
        , i, item, n, roomNO, data, $item, oName, oValue, options, option, story, sName, tedName, tedValue
        ;

    if(!sheep.length){
        return false;
    }
    for(i=0; i<sheep.length; i++){
        tedName = ''; // - Please select -';
        tedValue = '';
        item = sheep[i];
        $item = $(item);
        data = [];
        options = item.getElementsByTagName('option');
        sName = item.getAttribute('name');
        for(n=0; n<options.length; n++){
            option = options[n];
            oName = option.innerHTML;
            oValue = option.value;
            if(option.selected){
                tedName = oName;
                tedValue = oValue;
            }
            data.push({
                'name': oName,
                'value': oValue
            });
        }
        roomNO = GML.Room.checkin(data);
        story = [
            '<span class="select-box">',
                '<strong class="txt" role="mole-s" data-t="select-template" data-room="' + roomNO + '" data-name="' + sName + '"><a href="javascript:;"></a><em>' + tedName + '</em></strong>',
                '<input type="hidden" name="' + sName + '" value="' + tedValue + '">',
            '</span>'
        ].join('');
        $(story).insertAfter($item);
        $item.remove();
    }
}

$(doc).on('click', '[role~=option]', function(e){
    var room
        , $this = $(this)
        , BOSS = $this.closest('.dropDownMenu')
        // , $input = $BOSS.data('input')
        , trigger = BOSS.data('trigger')
        , field = BOSS.data('field')
        , callback = trigger.data('callback')
        , value = $this.data('value')
        ;

    /*
    if(!trigger){
        BOSS.siblings("[role=mole]")
        field = trigger.sibling("[name=" + trigger.data("name") +"]");
        BOSS.data("field", field);
    }
    if(!field){
        field = trigger.siblings("[name=" + trigger.data("name") +"]");
        BOSS.data("field", field);
    }
    */

    field.val(value);
    field.keyup();
    trigger.find('em').html($this.text());
    GML.Mole.shut();
    if(!callback){
        room = GML.Room[trigger.data("room")];
        if(room){
            callback = room["callback"];
        }
    }
    if(callback){
        callback({
            'input': field,
            'boss': BOSS,
            'option': $this,
            'value': value
        });
    }
    e.preventDefault();
});

/* Auto Spinner */
GML.Spinner = function(el){
    if(!el || !el.length){
        return false;
    }
    var $spinner = el.data('spinner')
        , pos, $target, height, BPtop
        ;

    if($spinner && !$spinner.is(':hidden')){
        $spinner.hide();
        return false;
    }
    if(!$spinner){
        $spinner = $('<div />', {
            'class': 'spinnerCenter'
        });
        $spinner.insertAfter(el);
        el.data('spinner', $spinner);
    }
    pos = el.position();
    height = el.outerHeight(true);
    if(height > 640){
        BPtop = '280px';
    }else{
        BPtop = 'center';
    }
    $spinner.css({
        'top': pos.top,
        'left': pos.left,
        'width': el.outerWidth(true),
        'height': height,
        'backgroundPosition': 'center ' + BPtop
    });
    $spinner.show();
}

GML.When = function(w, d){
    var when, TO;

    setTimeout(function(){
        when = w();

        TO = setInterval(function(){
            if(when){
                clearInterval(TO);
                d(when);
            }
        }, 300);

    }, 40);

}

/* Count Text */
GML.countStr = function(s){
    var i, length = 0;

    for(i=0; i<s.length; i++){
        if(s.charCodeAt(i)>127 || s.charCodeAt==94){
            length += 2;
        }else{
            length++;
       }
    }
    return length;
}

/* Teaser Text */
GML.Teaser = function(t, n){
    t = $.trim(t);

    if(t.length > n){
        t = t.substring(0, n - 4) + '...';
    }
    return t;
};

/* URL */
(function(){

    var baseURL = "/gmvo/"
        , gmlURL = {
            base: baseURL,
            product: baseURL + "product/",
            subaccount: baseURL + "subaccount/",
            message: baseURL + "message/",
            buyinglead: baseURL + "buyinglead/"
        }
        ;
    GML.URL = gmlURL;

})();

/* auth */
GML.auth = {};
